package com.squid.base.common.annotation.format;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.squid.base.common.annotation.time.CheckMethodTime;
import com.squid.base.common.error.ExceptionFactory;
import com.squid.base.utils.ValidatorUtils;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;

/**
 * 检测方法执行时间
 *
 * @author jh
 * @date 2022/12/6 16:25
 */
@Slf4j
@Component
@Aspect
public class FormatValidator {
    @Pointcut("@annotation(com.squid.base.common.annotation.format.FormatValid)")
    public void myPointcut() {

    }

    @Around("@annotation(formatValid)")
    public Object isAccessMethod(ProceedingJoinPoint joinPoint, FormatValid formatValid) {
        Object[] args = null;
        try {
             args = joinPoint.getArgs();
        } catch (Throwable e) {
            throw ExceptionFactory.instanceServerException("FormatValid获取对象失败", e);
        }
        for (Object obj : args) {
            Class<?> aClass = obj.getClass();
            Field[] fields = aClass.getDeclaredFields();
            for (Field field : fields) {
                if (field.isAnnotationPresent(FormatValid.class)) {
                    field.setAccessible(true);
                    FormatValid fieldFormatValid = field.getAnnotation(FormatValid.class);
                    String value = null;
                    try {
                        value = (String) field.get(obj);
                    } catch (IllegalAccessException e) {
                        throw ExceptionFactory.instanceServerException("FormatValid获取参数值失败", e);
                    }
                    if (formatValid.phone()) {
                        if (fieldFormatValid.phone()){
                            if (!ValidatorUtils.isPhone(value)) {
                                throw ExceptionFactory.instanceServerException("手机号格式不正确");
                            }
                        }
                    } else if (formatValid.idCard()) {
                        if (fieldFormatValid.idCard()){
                            if (!ValidatorUtils.idCardLengthUtil(value)) {
                                throw ExceptionFactory.instanceServerException("身份证号格式不正确");
                            }
                        }
                    } else {
                        throw ExceptionFactory.instanceServerException("未找到对应的参数校验类型");
                    }
                }
            }
        }
        try {
            return joinPoint.proceed(joinPoint.getArgs());
        } catch (Throwable e) {
            throw ExceptionFactory.instanceServerException(e.getMessage(),e);
        }
    }
}
